'''
Created on 03-Apr-2010

@author: darsh
'''

from Util import util
         
class VirtualDesktopManager:
    """
    This clas  manages the virtual desktops and handles switching between them and moving the client windows across desktops
    """
    def __init__(self):
        self.NUMBER_OF_DESKTOPS = 2
        self.currentdesktop = 1
        self.FOCUSSTACKS = []
        for desk in range(self.NUMBER_OF_DESKTOPS):
            self.FOCUSSTACKS.append([])
        
    

    def switchtodesktop(self, targetdesktop):
        if targetdesktop > 0 and targetdesktop <= self.NUMBER_OF_DESKTOPS:
            util.message("Switching to desktop " + str(targetdesktop))
            currentdesktop = self.currentdesktop
            self.DisableMasks()
            for client in self.FOCUSSTACKS[currentdesktop - 1]:
                client.unmap()
                self.CLIENTS[client].unframe()
            for client in self.FOCUSSTACKS[targetdesktop - 1]:
                if not self.CLIENTS[client].iconified:
                    client.map()   
                    self.CLIENTS[client].frame()
            self.currentdesktop = targetdesktop
            self.EWMH.setEWMHcurrentdesktop(targetdesktop - 1)
            self.drawwindows()
            self.EnableMasks()
               

    def sendwindowtodesktop(self, window, targetdesktop):
        if self.CLIENTS.has_key(window):
            if targetdesktop > 0 and targetdesktop <= self.NUMBER_OF_DESKTOPS:
                currentdesktop = self.CLIENTS[window].desktop
                self.removefromfocusstack(window, currentdesktop)
                self.pushonfocusstack(window, targetdesktop)
                self.CLIENTS[window].desktop = targetdesktop
                window.unmap()
                # print self.FOCUSSTACKS[self.currentdesktop-1]
                self.EWMH.setEWMHwindowdesktop(window, targetdesktop - 1)
                self.drawwindows()
                #Map other windows             
                                          
                

    def drawwindows(self):
        for client in self.FOCUSSTACKS[self.currentdesktop - 1]:
            if not self.CLIENTS[client].iconified:
                # print "MAPPING WINDOW" + str(client)
                client.map()
